package com.ignou.mca.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class SessionUtil
{
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "465";
	private static final String emailFromAddress = "infoatonlinevoting@gmail.com";
	private static final String emailFromPassword = "OnlineVoting";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

	private Logger log4j;
	{
		log4j = Logger.getLogger(SessionUtil.class);
	}
	public SessionUtil() {
		// TODO Auto-generated constructor stub
		log4j = Logger.getLogger(SessionUtil.class);
	}


	public static boolean validateSession(HttpServletRequest request, 		HttpServletResponse response){
		HttpSession session = request.getSession(false);
		if (session==null ||session.isNew()){
			return false;
		}
		return true;
	} 
	
	
	public static void sendMail(String email,String userid,String passwd,String purpose,String random) throws AddressException,MessagingException
	{
		// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
//		String to = email;
//		String from = "infoatonlinevoting@gmail.com";
//		// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
//		String host = "smtp.gmail.com";
//		// Create properties, get Session
//		Properties props = new Properties();
//		// If using static Transport.send(),
//		// need to specify which host to send it to
//		props.put("mail.smtp.host", host);
//		// To see what is going on behind the scene
//		props.put("mail.debug", "true");
//		Session session = Session.getInstance(props);
//		try {
//			// Instantiatee a message
//			MimeMessage msg = new MimeMessage(session);
//			//Set message attributes
//			msg.setFrom(new InternetAddress(from));
//			InternetAddress[] address = {new InternetAddress(to)};
//			msg.setRecipients(MimeMessage.RecipientType.TO, address);
//			
//			msg.setSentDate(new Date());
//			if(purpose.equalsIgnoreCase("register"))
//			{
//				msg.setSubject("[Secure Online Voting]:Your New Account Detalis");
//				// Set message content
//				msg.setText("Hi "+userid+", \nYou've been successfully registered for Online Voting \nUserId: "+userid+"\nPassWord: "+passwd+
//						"\n Please feel free to send your comments to : "+from);
//			}
//			else if (purpose.equalsIgnoreCase("voted"))
//			{
//				msg.setSubject("[Secure Online Voting]: Thank You for Voting");
//				// Set message content
//				msg.setText("Hi "+userid+", \nYou've been successfully voted to your favourite candidate(s)." +
//						"\n Tell us your feedback @ "+from);
//				
//			}
//			else if(purpose.equalsIgnoreCase("registrationsuccess"))
//			{
//				msg.setSubject("[Secure Online Voting]: Registration Successful");
//				// Set message content
//				msg.setText("Hi "+userid+", \n Thank you for Registering Online voting" +
//						"");
//			}
//			else
//			{
//				msg.setSubject("[Secure Online Voting]: Forgot Password");
//				// Set message content
//				msg.setText("Hi "+userid+", \nYour Password is : "+passwd);
//				
//			}
//			
//			//Send the message
//			Transport.send(msg);
//		}
//		catch (MessagingException mex) {
//			// Prints all nested (chained) exceptions as well
//			mex.printStackTrace();
//		}
		
		
		
		boolean debug = true;

		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {

					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(emailFromAddress, emailFromPassword);
					}
				});

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(emailFromAddress);
		msg.setFrom(addressFrom);

		String[] recipients = new String[1];
		recipients[0] = email;
		
		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		if(purpose.equalsIgnoreCase("register"))
			{
				msg.setSubject("[Secure Online Voting]:Your New Account Detalis");
				// Set message content
				msg.setText("Hi "+userid+", \nYou've been successfully registered for Online Voting \nUserId: "+userid+"\nPassWord: "+passwd+
						"\n Please feel free to send your comments to : "+emailFromAddress);
			}
			else if (purpose.equalsIgnoreCase("voted"))
			{
				msg.setSubject("[Secure Online Voting]: Thank You for Voting");
				// Set message content
				msg.setText("Hi "+userid+", \nYou've been successfully voted to your favourite candidate(s)." +
						"\n Tell us your feedback @ "+emailFromAddress);
				
			}
			else if(purpose.equalsIgnoreCase("registrationsuccess"))
			{
				msg.setSubject("[Secure Online Voting]: Registration Successful");
				// Set message content
				msg.setText("Hi "+userid+", \n Thank you for Registering Online voting" +
						"");
			}
			else
			{
				msg.setSubject("[Secure Online Voting]: Forgot Password");
				// Set message content
				msg.setText("Hi "+userid+", \nYour Password is : "+passwd);
				
			}
		//msg.setSubject(subject);
		//msg.setContent(message, "text/plain");
		Transport.send(msg);
		
		
		
	}


	public static boolean logOut(HttpServletRequest request, HttpServletResponse response){
		request.getSession().invalidate();
		return true;
	}
	
	public static void main(String args[]) throws Exception {
		sendMail("venu.2703@gmail.com", "user", "ds", "register", "XDYAJDF");
	}

}